<template>
  <div class="layout-container">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="logo">
          <h2 v-if="!isCollapse">Fish System</h2>
          <h2 v-else>F</h2>
        </div>
        <DynamicMenu :is-collapse="isCollapse" />
      </el-aside>

      <!-- 主区域 -->
      <el-container>
        <!-- 顶部导航栏 -->
        <el-header>
          <div class="header-content">
            <div class="header-left">
              <el-icon class="collapse-icon" @click="toggleCollapse">
                <component :is="isCollapse ? 'Expand' : 'Fold'" />
              </el-icon>
              <Breadcrumb />
            </div>
            <div class="header-right">
              <!-- 主题切换按钮 -->
              <el-tooltip :content="themeStore.isDark ? '切换到日间模式' : '切换到夜间模式'" placement="bottom">
                <el-icon class="theme-icon" @click="themeStore.toggleTheme()">
                  <component :is="themeStore.isDark ? 'Sunny' : 'Moon'" />
                </el-icon>
              </el-tooltip>

              <el-dropdown @command="handleCommand">
                <span class="user-dropdown">
                  <el-avatar :size="35" :src="userInfo?.avatar || defaultAvatar">
                    {{ userInfo?.nickname?.charAt(0) || 'U' }}
                  </el-avatar>
                  <span class="username">{{ userInfo?.nickname || userInfo?.username }}</span>
                  <el-icon><ArrowDown /></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="profile">
                      <el-icon><User /></el-icon>
                      个人信息
                    </el-dropdown-item>
                    <el-dropdown-item command="refresh">
                      <el-icon><Refresh /></el-icon>
                      刷新权限
                    </el-dropdown-item>
                    <el-dropdown-item command="logout" divided>
                      <el-icon><SwitchButton /></el-icon>
                      退出登录
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>

        <!-- 标签栏 -->
        <TagsView />

        <!-- 内容区域 -->
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useThemeStore } from '@/stores/theme'
import DynamicMenu from '@/components/DynamicMenu.vue'
import Breadcrumb from '@/components/Breadcrumb.vue'
import TagsView from '@/components/TagsView.vue'
import { ArrowDown, User, SwitchButton, Expand, Fold, Sunny, Moon, Refresh } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()
const themeStore = useThemeStore()

const userInfo = computed(() => userStore.userInfo)
const defaultAvatar = 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
const isCollapse = ref(false)

// 切换侧边栏折叠
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 下拉菜单命令处理
const handleCommand = (command) => {
  if (command === 'profile') {
    router.push('/profile')
  } else if (command === 'refresh') {
    userStore.refreshUserPermissions()
  } else if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      userStore.logout()
    }).catch(() => {
      // 取消操作
    })
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  overflow: hidden;
}

.el-container {
  height: 100%;
}

.el-aside {
  background: #001529;
  transition: width 0.3s;
  overflow-x: hidden;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  font-weight: bold;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo h2 {
  margin: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.el-header {
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 0;
  height: 60px;
  display: flex;
  align-items: center;
}

.header-content {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.collapse-icon,
.theme-icon {
  font-size: 20px;
  cursor: pointer;
  transition: color 0.3s;
  margin-right: 15px;
}

.collapse-icon:hover,
.theme-icon:hover {
  color: #667eea;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 8px;
  transition: background 0.3s;
}

.user-dropdown:hover {
  background: #f5f7fa;
}

.username {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.el-main {
  background: #f5f7fa;
  overflow-y: auto;
  padding: 0;
}

:deep(.el-menu) {
  border-right: none;
  background: #001529;
}

:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  color: rgba(255, 255, 255, 0.7);
}

:deep(.el-menu-item:hover),
:deep(.el-sub-menu__title:hover) {
  background-color: rgba(255, 255, 255, 0.1);
  color: white;
}

:deep(.el-menu-item.is-active) {
  background-color: #667eea !important;
  color: white !important;
}

/* 暗黑模式样式 */
.dark .el-aside {
  background: #1a1a1a;
}

.dark .el-header {
  background: #2c2c2c;
  color: #e5e7eb;
}

.dark .el-main {
  background: #141414;
}

.dark .logo {
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.dark .user-dropdown:hover {
  background: rgba(255, 255, 255, 0.1);
}

.dark .username {
  color: #e5e7eb;
}

.dark .collapse-icon:hover,
.dark .theme-icon:hover {
  color: #818cf8;
}
</style>